package com.zzyl.controller.customer;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.controller.BaseController;
import com.zzyl.dto.ReservationDTO;
import com.zzyl.service.ReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.ReservationCountByTimeVo;
import com.zzyl.vo.ReservationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;

@RestController
@RequestMapping("/customer/reservation")
@Api(tags = "客户预约管理")
public class CustomerReservationController extends BaseController {
    @Autowired
    private ReservationService reservationService;

    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public ResponseResult<Integer> getCancelledReservationCount() {
        String userId = UserThreadLocal.getSubject();
        System.out.println(userId+"---------------------");
        Integer i = reservationService.getCancelledReservationCount();
        return success(i);
    }


    @ApiOperation("查询时间段剩余预约次数")
    @GetMapping("/countByTime")
    public ResponseResult<Integer> getCountByTime(Long time) {
        System.out.println("==============="+time+"================");
        Date date = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String times = sdf.format(date);
        ReservationCountByTimeVo vo = reservationService.getCountByTime(times);
        return success(vo);
    }

    @ApiOperation("新增预约")
    @PostMapping()
    public ResponseResult addReservation(@RequestBody ReservationDTO dto){
        String time = dto.getTime();
        reservationService.addReservation(dto);
        return success();
    }


    @ApiOperation("分页查询预约")
    @GetMapping("/page")
    public ResponseResult getList(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                  @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                                  @RequestParam(value = "status" ,required = false) Integer status
    ){
        PageResponse vo = reservationService.getList(pageNum,pageSize,status);
        return  success(vo);
    }

    @ApiOperation("取消预约")
    @PutMapping("/{id}/cancel")
    public ResponseResult cancleReservation(@PathVariable Integer id){
        reservationService.cancleReservation(id);
        return success();
    }
}